<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\OperationLogs;
use App\Model\Roles;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RoleController extends BaseController
{
    public function __construct(Request $request, Roles $roles)
    {
        parent::__construct($request);
        $this->model = $roles;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getList($request));
    }

    public function getAllRole(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getAllRole());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required|unique:mysql.roles,name|max:50'
        ],[
            'required' => '角色名不可为空',
            'unique' => '角色名已存在',
            'max' => '角色名不可超过50个字符'
        ]);

        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }
        $role = Role::create($request->only(['name','desc']));
        OperationLogs::createData($request,OperationLogs::ROLE, '新增角色');
        return $this->toJson(self::SUCCESS,$role);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $res = Role::find($id)->permissions->pluck('id');
        return $this->toJson(self::SUCCESS,$res);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        Role::where(['id' => $id])->update($request->only('name','desc'));
        OperationLogs::createData($request,OperationLogs::ROLE, '编辑角色',$id);
        return $this->toJson();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        $role = Role::find($id);
        $permission = new Permission();
        $permission->removeRole($role);

        $role->destroy($id);
        OperationLogs::createData($request,OperationLogs::ROLE, '删除角色', $id);
        return $this->toJson();
    }

    public function permission(Request $request){
        $validator = Validator::make($request->all(),[
            'permission' => 'required',
            'role_id' => 'required'
        ],[
            'permission.required' => '权限不可为空',
            'role_id.required' => '角色ID不可为空',
        ]);
        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, $validator->errors()->all()[0]);
        }
        $role = Role::findById($request->post('role_id'));
        $role->permissions()->sync($request->post('permission'));
        OperationLogs::createData($request,OperationLogs::ROLE, '分配权限');
        return $this->toJson();
    }

    public function front(){
        return $this->user->getDirectPermissions();
    }
}
